import { defineStore } from 'pinia'
import { ref } from 'vue'
import { type setObj, type cart } from '@/typings'
import { Dialog, Snackbar } from '@varlet/ui'



export const useUserStore = defineStore('big-user', () => {
    const token = ref('eeejjj');
    const name = ref('');
    const address = ref<Array<setObj>>([]);
    const id = ref(0)
    const shopCar = ref<Array<cart>>([])
    const checked = ref()

    const setToken = (newToken: string) => {

        token.value = newToken
    }

    const removeToken = () => {
        token.value = ''
    }
    const removeId = () => {
        if(id.value<=0){
            id.value =0
        }
        id.value--
    }
    const addId = () => {
        id.value++
    }
    const setName = (username: string) => {
        name.value = username
    }
    const setAddress = (item: setObj) => {
        if (item.isDefault === true) {
            for (let i = 0; i < address.value.length; i++) {
                address.value[i].isDefault = false;
            }
            address.value.unshift(item)
        } else {
            address.value.push(item)
        }

    }

    const setShopCar = (item: cart) => {

        for (let i = 0; i < shopCar.value.length; i++) {
            if (item.name == shopCar.value[i].name && item.level == shopCar.value[i].level && item.color == shopCar.value[i].color) {
                shopCar.value[i].number += item.number
                return
            }
        }
        shopCar.value.push(item)
    }
    const actions: { [key: string]: () => void } = {
        confirm: () => {
            let indices = [];
            for (let i = 0; i < shopCar.value.length; i++) {
                for (let j = 0; j < checked.value.length; j++) {
                    if (shopCar.value[i].name === checked.value[j].name) {
                        indices.push(i);
                    }
                }
            }
            for (let k = indices.length - 1; k >= 0; k--) {
    
    
                shopCar.value.splice(indices[k], 1);
            }
            Snackbar.success('删除成功')
        },
        cancel: () => Snackbar.error('取消删除'),
    };
    const clearShopCar = async (select) => {
        checked.value = select
        actions[await Dialog('是否要删除')]();
    }
    const addCartNum = (index: number, num: number) => {
        shopCar.value[index].number = num
    }
    const clearCar = (item)=>{
        checked.value = item
        let indices = [];
            for (let i = 0; i < shopCar.value.length; i++) {
                for (let j = 0; j < checked.value.length; j++) {
                    if (shopCar.value[i].name === checked.value[j].name && shopCar.value[i].color === checked.value[j].color && shopCar.value[i].level === checked.value[j].level ) {
                        indices.push(i);
                    }
                }
            }
            for (let k = indices.length - 1; k >= 0; k--) {
                shopCar.value.splice(indices[k], 1);
            }
    }
    return {
        token,
        name,
        setToken,
        removeToken,
        setName,
        address,
        setAddress,
        id,
        removeId,
        addId,
        shopCar,
        setShopCar,
        clearShopCar,
        addCartNum,
        clearCar

    }
}, {
    persist: true,
})